/*******************************************************************************
 * Simple C-K Editor
 * 
 * Copyright (C) 2009 Ingi BROWN - Centre de Gestion Scientifique - Ecole des Mines ParisTech
 * Licensed under the GNU General Public License Version 3.
 * 
 * Simple C-K Editor includes source code and libraries from the JUNG Project (Java Universal Network/Graph Framework) 
 * released under the BSD license. Copyright (c) 2003-2004,  Regents of the University of California and the JUNG Project.
 * 
 * See the License.txt file for more details.
 ******************************************************************************/

package edu.ensmp.ckEditor.main;

import javax.swing.JPopupMenu;

import edu.ensmp.ckEditor.ckModel.CKModel;
import edu.ensmp.ckEditor.ckModel.CKModel.Concept;
import edu.ensmp.ckEditor.ckModel.CKModel.Knowledge;
import edu.ensmp.ckEditor.gui.EditorFrame;
import edu.ensmp.ckEditor.gui.Genius;
import edu.ensmp.ckEditor.gui.Graph;
import edu.ensmp.ckEditor.mousePlugin.EditingModalGraphMouse;
import edu.ensmp.ckEditor.mousePlugin.MyMouseMenus;
import edu.ensmp.ckEditor.mousePlugin.PopupVertexEdgeMenuMousePlugin;
import edu.ensmp.ckEditor.util.Constants;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;


/**
 * Application Launcher with its main method.
 */
public class Launcher {
	
	private static CKModel model;

	private static Logger logger;

	/**
	 * We store the main window in this private variable to be able to access
	 * information such as window location, size etc. from subclasses.
	 */
	private static EditorFrame editorGUI;

	private static Genius genius;

	/**
	 * Set up the mouse manager for each graph (concept and knowledge).
	 */
	/*
	 * This method is not very elegant, I tried to detail the steps as best as
	 * possible, but it might be a little hard to grasp on a first hand. You can
	 * skip this if you are discovering the source as it is not the most
	 * important. However if you have problems with mouse behavior, this is
	 * where the managers get set up. Check the mousePlugin package also as that
	 * is where the right-click is managed.
	 */
	
	static edu.ensmp.ckEditor.mousePlugin.EditingModalGraphMouse<Concept, Integer> conceptGraphMouse;

	static edu.ensmp.ckEditor.mousePlugin.EditingModalGraphMouse<Knowledge, Integer> knowledgeGraphMouse;

	/**
	 * The Main method: launch the application and show the main window
	 * 
	 * @param args
	 *            are ignored by this application.
	 */
	public static void main(String[] args) {
		
		// This is only for apple and will not harm the other
		System.setProperty("apple.laf.useScreenMenuBar", "true");
		System.setProperty("com.apple.mrj.application.apple.menu.about.name", Constants.APP_NAME);
		
		model = new CKModel();
		
		setUpLogger();
		
		// Create and display the GUI
		createAndDisplay();

		// Set up our custom mouse manager for each panel
		setUpMouse();

		// Set up Genius Window
		// createAndDisplayGenius();

	}

	private static void setUpLogger() {
		logger = new Logger();
	}

	/* Getters and setters */
	/**
	 * Create the GUI and display it.
	 */
	private static void createAndDisplay() {
	
		// Create and set up the CKwindow.
		editorGUI = new EditorFrame(model);
	}

	@SuppressWarnings("unused")
	private static void createAndDisplayGenius() {
		genius = new Genius();
	}

	private static void setUpMouse() {
	
		// --- CONCEPTS --- //
	
		// First we get the visualization viewer and the graph.
		VisualizationViewer<Concept, Integer> conceptVisuViewer = getEditorGUI()
				.getConceptPanel().getVisuViewer();
		
		Graph<Concept, Integer> conceptGraph = getEditorGUI().getConceptPanel()
				.getGraph();
	
		// Create a graph mouse and add it to the visualization component
		conceptGraphMouse = new edu.ensmp.ckEditor.mousePlugin.EditingModalGraphMouse<Concept, Integer>(
				conceptVisuViewer.getRenderContext(),
				conceptGraph.conceptFactory, conceptGraph.edgeFactory);
		// Set the mouse to our concept Graph
		conceptVisuViewer.setGraphMouse(conceptGraphMouse);
		// Start off in transforming mode
		conceptGraphMouse.setMode(ModalGraphMouse.Mode.EDITING);
		// Key listener module to use 't' and 'p' keys for transform/pick
		// mode...
		conceptVisuViewer
				.addKeyListener(conceptGraphMouse.getModeKeyListener());
	
		// --- Right click mouse plugin --- //
	
		// Create our own mouse plugin
		PopupVertexEdgeMenuMousePlugin<Concept, Integer> conceptPopupMousePlugin = new PopupVertexEdgeMenuMousePlugin<Concept, Integer>();
	
		// Add some popup menus for the edges and vertices to our mouse plugin.
		final JPopupMenu edgeMenu = new MyMouseMenus.EdgeMenu(getEditorGUI()
				.getEditorFrame());
		final JPopupMenu conceptMenu = new MyMouseMenus.ConceptMenu(
				getEditorGUI().getEditorFrame());
	
		conceptPopupMousePlugin.setEdgePopup(edgeMenu);
		conceptPopupMousePlugin.setVertexPopup(conceptMenu);
	
		// Get the existing mouse controller on concept graph
		conceptGraphMouse.remove(conceptGraphMouse.getPopupEditingPlugin()); // Removes
																				// the
																				// existing
																				// popup
																				// editing
																				// plugin
		conceptGraphMouse.add(conceptPopupMousePlugin); // Add our own new
														// plugin to the mouse
	
		// --- KNOWLEDGE --- // --> Is exactly the same as above but for the
		// right panel: Knowledge Panel
	
		// First we get the visualization viewer and the graph.
		VisualizationViewer<Knowledge, Integer> knowledgeVisuViewer = getEditorGUI()
				.getKnowledgePanel().getVisuViewer();
		Graph<Knowledge, Integer> knowledgeGraph = getEditorGUI()
				.getKnowledgePanel().getGraph();
	
		// Create a graph mouse and add it to the visualization component
		knowledgeGraphMouse = new edu.ensmp.ckEditor.mousePlugin.EditingModalGraphMouse<Knowledge, Integer>(
				knowledgeVisuViewer.getRenderContext(),
				knowledgeGraph.knowledgeFactory, knowledgeGraph.edgeFactory);
		knowledgeVisuViewer.setGraphMouse(knowledgeGraphMouse);
		// Start off in editing mode
		knowledgeGraphMouse.setMode(ModalGraphMouse.Mode.EDITING);
		// Key listener module to use 't' and 'p' keys for transform/pick
		// mode...
		knowledgeVisuViewer.addKeyListener(knowledgeGraphMouse
				.getModeKeyListener());
	
		// --- Right click mouse plugin --- //
	
		// Create our own mouse plugin
		PopupVertexEdgeMenuMousePlugin<Knowledge, Integer> knowledgeMousePlugin = new PopupVertexEdgeMenuMousePlugin<Knowledge, Integer>();
	
		// Add knowledge vertex popup menu to our mouse plugin. (Edge menu is
		// the same as above)
		JPopupMenu knowledgeMenu = new MyMouseMenus.KnowledgeMenu(
				getEditorGUI().getEditorFrame());
	
		knowledgeMousePlugin.setEdgePopup(edgeMenu);
		knowledgeMousePlugin.setVertexPopup(knowledgeMenu);
	
		knowledgeGraphMouse.remove(knowledgeGraphMouse.getPopupEditingPlugin()); // Removes
																					// the
																					// existing
																					// popup
																					// editing
																					// plugin
		knowledgeGraphMouse.add(knowledgeMousePlugin); // Add our own new plugin
														// to the mouse
	}
	
	public static void restart(){
		
		// Set up my logger
		setUpLogger();

		// Create and display the GUI
		createAndDisplay();

		// Set up our custom mouse manager for each panel
		setUpMouse();

		// Set up Genius Window
		// createAndDisplayGenius();
	}

	/**
	 * @return the editorGUI
	 */
	public static EditorFrame getEditorGUI() {
		return editorGUI;
	}

	/**
	 * @return the genius
	 */
	public static Genius getGenius() {
		return genius;
	}

	/**
	 * @return the logger
	 */
	public static Logger getLogger() {
		return logger;
	}
	
	public static EditingModalGraphMouse<Concept, Integer> getConceptGraphMouse() {
		return Launcher.conceptGraphMouse;
	}
	
	public static EditingModalGraphMouse<Knowledge, Integer> getKnowledgeGraphMouse() {
		return Launcher.knowledgeGraphMouse;
	}
}
